package org.xlx.xlx_oa.attendance.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xlx.xlx_oa.attendance.convertor.AttendanceConvertor;
import org.xlx.xlx_oa.attendance.domain.Attendance;
import org.xlx.xlx_oa.attendance.domain.vo.AttendanceVo;
import org.xlx.xlx_oa.attendance.mapper.AttendanceMapper;
import org.xlx.xlx_oa.common.Constant;
import org.xlx.xlx_oa.role_user.service.AdmProcessService;
import org.xlx.xlx_oa.utils.ThreadLocalUtil;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;

@Service
public class AttendanceService {
    private final AttendanceConvertor attendanceConvertor = AttendanceConvertor.INSTANCE;
    @Autowired
    private AttendanceMapper attendanceMapper;
    @Autowired
    private AdmProcessService admProcessService;

    /**
     * 日常考勤
     *
     * @param attendanceVo
     * @return
     */
    public String checkAttendance(AttendanceVo attendanceVo) {
        LocalDateTime signInTime = attendanceVo.getSignInTime();
        LocalDateTime attendDate = LocalDateTime.of(signInTime.toLocalDate(), LocalTime.MIN);
        LocalDateTime point = attendDate.plusHours(12);
        String result = null;
        QueryWrapper<Attendance> wrapper = new QueryWrapper<>();
        Object objUserId = ThreadLocalUtil.get(Constant.USERID);
        Long userId = Objects.isNull(objUserId) ? null : Long.valueOf(objUserId.toString());
        wrapper.lambda()
                .eq(Attendance::getCreatedId, userId)
                .eq(Attendance::getAttendanceDate, attendDate);
        List<Attendance> attendances = attendanceMapper.selectList(wrapper);
        if (attendances != null && attendances.size() != 0) {
            Attendance attendance = attendances.get(0);
            if (signInTime.isBefore(point)) {
                if (attendance.getOnDutyTime() != null) {
                    DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                    result = String.format("重复签到，签到时间: %s", df.format(attendance.getOnDutyTime()));
                    return result;
                } else {
                    attendance.setOnDutyTime(signInTime);
                    attendanceMapper.updateById(attendance);

                    if (signInTime.isAfter(attendDate.plusHours(9))) {
                        Duration duration = Duration.between(attendDate.plusHours(9), signInTime);
                        Long minutes = duration.toMinutes();
                        result = String.format("签到成功，迟到 %s 分钟", minutes);
                    } else {
                        result = "签到成功";
                    }
                    return result;
                }
            } else {
                if (attendance.getOffDutyTime() != null) {
                    DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                    result = String.format("重复签退，签退时间: %s", df.format(attendance.getOffDutyTime()));
                    return result;
                } else {
                    attendance.setOffDutyTime(signInTime);
                    attendanceMapper.updateById(attendance);

                    if (signInTime.isBefore(attendDate.plusHours(18))) {
                        Duration duration = Duration.between(signInTime, attendDate.plusHours(18));
                        Long minutes = duration.toMinutes();
                        result = String.format("签退成功，早退 %s 分钟", minutes);
                    } else {
                        result = "签退成功";
                    }
                    return result;
                }
            }
        } else {
            Attendance attendance = new Attendance();
            attendance.setAttendanceDate(attendDate);
            if (signInTime.isBefore(point)) {
                attendance.setOnDutyTime(signInTime);
                attendanceMapper.insert(attendance);

                if (signInTime.isAfter(attendDate.plusHours(9))) {
                    Duration duration = Duration.between(attendDate.plusHours(9), signInTime);
                    Long minutes = duration.toMinutes();
                    result = String.format("签到成功，迟到 %s 分钟", minutes);
                } else {
                    result = "签到成功";
                }
                return result;
            } else {
                attendance.setOffDutyTime(signInTime);
                attendanceMapper.insert(attendance);

                if (signInTime.isBefore(attendDate.plusHours(18))) {
                    Duration duration = Duration.between(signInTime, attendDate.plusHours(18));
                    Long minutes = duration.toMinutes();
                    result = String.format("签退成功，早退 %s 分钟", minutes);
                } else {
                    result = "签退成功";
                }
                return result;
            }
        }
    }
}
